import { defineStore } from 'pinia'

/* 商品添加 */
export default defineStore('cacheStore', {
	state: () => ({
		data: {
			cates: [] as object[],
			countrys: [] as object[],
			regions: [] as object[],
			banks: [] as object[],
			BankCities: [] as object[],
			brands: [] as object[],
			units: [] as object[],
			exprs: [] as object[]
		},
		paths: {
			cates: 'cate/tree',
			countrys: 'country/tree',
			brands: 'brand',
			units: 'unit',
			regions: 'city/tree',
			banks: 'bank',
			citys: 'city/tree'
		},
		loading: false
	}),
	getters: {
		cates(): object[] {
			if(this.data.cates.length == 0){
				this.loading = true
				uni.$u.http.get(this.paths.cates).then((data: object[]) => {
					this.loading = false
					this.data.cates = data
				})
			}
			return this.data.cates
		},
		regions(): object[]{
			if(this.data.regions.length == 0){
				this.loading = true
				uni.$u.http.get(this.paths.regions).then((data: object[]) => {
					this.loading = false
					this.data.regions = data
				})
			}
			return this.data.regions
		},
		countrys(): object[]{
			if(this.data.countrys.length == 0){
				this.loading = true
				uni.$u.http.get(this.paths.countrys).then((data: object[]) => {
					this.loading = false
					this.data.countrys = data
				})
			}
			return this.data.countrys
		},
		banks(): object[]{
			if(this.data.banks.length == 0){
				this.loading = true
				uni.$u.http.get(this.paths.banks).then((data: object[]) => {
					this.loading = false
					this.data.banks = data
				})
			}
			return this.data.banks
		},
		brands(): object[]{
			if(this.data.brands.length == 0){
				this.loading = true
				uni.$u.http.get(this.paths.brands).then((data: object[]) => {
					this.loading = false
					this.data.brands = data
				})
			}
			return this.data.brands
		}
	},
	actions: {
    update(values: object) {
			Object.assign(this, values)
    },
		load(index: string) {
			const presell = uni.$store.main.idtype == 'store' && uni.$store.store.presell == 1
			const paths = {
				cate: 'matter/cate/tree', 
				region: 'region/tree', 
				country: 'matter/country', 
				bank: 'index/bank',
				expr: 'index/express/tree',
				BankCity: 'index/bank/tree'
			} 
			uni.$u.http.get(paths[index], {presell: presell ? 1 : 0}).then((res: {list: object[]}) => {
				this[index + 's'] = res.list
			})
		},
		init() {
			uni.$u.http.get('index/filter').then(res => {
				Object.assign(this, res)
			})
		}
	}
})